package com.younion.action.manage;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.younion.business.IMemberCardService;
import com.younion.business.IMsgTemplateService;
import com.younion.business.INotificationTaskLogService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.Status;
import com.younion.common.constants.TemplateTargetType;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.MsgTemplateVO;
import com.younion.vo.NotificationTaskLogVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author Joe Chen
*/
public class NotificationTaskLogAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(NotificationTaskLogAction.class);

	private INotificationTaskLogService notificationTaskLogService = null;

	private NotificationTaskLogVO notificationTaskLogVO = new NotificationTaskLogVO();

	private List<NotificationTaskLogVO> notificationTaskLogList; 
	
	private List<MsgTemplateVO> templates = null;
	
	private Collection membercards = null;
	
	private List<TemplateTargetType> templateTargetTypes = Arrays.asList(TemplateTargetType.values());
	
	@Autowired
	private IMsgTemplateService msgTemplateService = null;
	@Autowired
	private IMemberCardService memberCardService = null;
	
	private String searchType;
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listNotificationTaskLog() {
		try {
			Map paramMap = null;
			if (notificationTaskLogVO != null) {
				if(notificationTaskLogVO.getTemplate() != null && "".equals(notificationTaskLogVO.getTemplate().getTargetType()))
					notificationTaskLogVO.getTemplate().setTargetType(null);
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(notificationTaskLogVO));
			}
			// 获取总记录数
			int total = notificationTaskLogService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			notificationTaskLogList = notificationTaskLogService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 *新增
	 */
	public String addNotificationTaskLog() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	public String editNotificationTaskLog() {
		try {
			notificationTaskLogVO = notificationTaskLogService.getById(notificationTaskLogVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	public String saveNotificationTaskLog() {
		try {
			notificationTaskLogVO = notificationTaskLogService.save(notificationTaskLogVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteNotificationTaskLog() {
		try {
			String pkId = request.getParameter("key");
			notificationTaskLogService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	public String viewNotificationTaskLog() {
		try {
			String pkId = request.getParameter("key"); 
			notificationTaskLogVO = notificationTaskLogService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public NotificationTaskLogVO getNotificationTaskLogVO() {
		return notificationTaskLogVO;
	}

	public void setNotificationTaskLogVO(NotificationTaskLogVO notificationTaskLogVO) {
		this.notificationTaskLogVO = notificationTaskLogVO;
	}

	public void setNotificationTaskLogService(INotificationTaskLogService notificationTaskLogService) {
		this.notificationTaskLogService = notificationTaskLogService;
	}

	public List<NotificationTaskLogVO> getNotificationTaskLogList() {
		return notificationTaskLogList;
	}

	public void setNotificationTaskLogList(List<NotificationTaskLogVO> notificationTaskLogList) {
		this.notificationTaskLogList = notificationTaskLogList;
	}
	
	public List<MsgTemplateVO> getTemplates() {
		if(templates == null){
			templates = Lists.newArrayList();
			Map paramMap = Maps.newHashMap();
			try {
				//paramMap.put("status",Status.APPROVABLE.getName());
				paramMap.put("status",Status.ACTIVE.getName());
				//paramMap.put("scope", TemplateScope.MSG.getName());
				if(getSessionUser().getCustomer() != null){
					paramMap.put("customerId", getSessionUser().getCustomer().getId());
				}
				Collection msgs = msgTemplateService.getByMap(paramMap);
				Iterator it  = msgs.iterator();
				while(it.hasNext()){
					templates.add((MsgTemplateVO)it.next());
				}
			} catch (BaseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return templates;
	}

	public void setTemplates(List<MsgTemplateVO> templates) {
		this.templates = templates;
	}


	public IMsgTemplateService getMsgTemplateService() {
		return msgTemplateService;
	}

	public void setMsgTemplateService(IMsgTemplateService msgTemplateService) {
		this.msgTemplateService = msgTemplateService;
	}

	public IMemberCardService getMemberCardService() {
		return memberCardService;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

	public INotificationTaskLogService getNotificationTaskLogService() {
		return notificationTaskLogService;
	}
	
	public Collection getMembercards() {
		if(membercards == null){
			try {
				if(this.getSessionUser().getCustomer() == null){
					membercards = memberCardService.getAll();
				}else{
					Map paramMap = Maps.newHashMap();
					paramMap.put("customerId",this.getSessionUser().getCustomer().getId());
					membercards = memberCardService.getByMap(paramMap);
				}
			} catch (Exception e) {
			}
		}
		return membercards;
	}

	public void setMembercards(Collection membercards) {
		this.membercards = membercards;
	}

	public List<TemplateTargetType> getTemplateTargetTypes() {
		return templateTargetTypes;
	}

	public void setTemplateTargetTypes(List<TemplateTargetType> templateTargetTypes) {
		this.templateTargetTypes = templateTargetTypes;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}
		
	
}